package com.health.mapp.impl;

import java.util.List;

import org.apache.http.util.TextUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.health.db.model.PatientShortInfo;
import com.health.db.model.UserInfo;
import com.health.db.service.user.IPatientShortInfoService;
import com.health.db.service.user.IUserInfoService;
import com.health.mapp.bean.GetOpenIdMiNiReq;
import com.health.mapp.bean.GetOpenIdMiNiResp;
import com.health.mapp.bean.Header;
import com.health.mapp.utils.Response;
import com.health.mapp.utils.ResultCode;
import com.wxtools.api.IMiniService;
import com.wxtools.bean.MiniJsCode2SessionResult;

/**
 * 获取小程序userId
 * @author yuchong
 */
public class GetOpenIdMiNiImpl20180628 implements ActionInterface{

	@Autowired
    private IMiniService miniService;
	
	@Autowired
	private IUserInfoService userInfoService;
	
	@Autowired
	IPatientShortInfoService patientShortInfoService;
	
	
    
    private static Logger logger = Logger.getLogger(GetOpenIdMiNiImpl20180628.class);

    @Override
    public Response execute(Header header, Object reqBean) throws Exception {


    	GetOpenIdMiNiReq req = (GetOpenIdMiNiReq) reqBean;
    	GetOpenIdMiNiResp _resp = new GetOpenIdMiNiResp();

        if (TextUtils.isEmpty(req.getCode()))
        {
            _resp.setErrorCode(ResultCode.OPERATION_LAKE_PARAMETER_ERROR);
            _resp.setErrorMsg(ResultCode.OPERATION_LAKE_PARAMETER_ERROR_NOTE);

            return _resp;
        }

        try {
        	
        	MiniJsCode2SessionResult tokenResult=miniService.jsCode2SessionResult(req.getCode());
            
        	/*MiniJsCode2SessionResult tokenResult = new MiniJsCode2SessionResult();
        	tokenResult.setOpenid("123456");*/
        	
        	//判断是否获取token正确
        	if (null != tokenResult&&null!=tokenResult.getOpenid()) {
        		
        		List<UserInfo> list = userInfoService.selectByOpenId(tokenResult.getOpenid());
        		
        		if (list.size()>=1) {
					
        			_resp.setUserId(String.valueOf(list.get(0).getUserId()));
        			
        			 List<PatientShortInfo> patientList=patientShortInfoService.selectByUserId(list.get(0).getUserId());
        			 
        			 //_resp.setPatientList(patientList);
        			// _resp.setPatientSize(patientList.size());
        			
        		}else{
        			
        			UserInfo userInfo =new UserInfo();
        			
        			userInfo.setOpenId(tokenResult.getOpenid());
        			userInfo.setUnionId(tokenResult.getUnionid());
        			userInfo=userInfoService.insert(userInfo);
        			_resp.setUserId(String.valueOf(userInfo.getUserId()));
        			//_resp.setPatientSize(0);
        		}
        		
				
			}else{
		         _resp.setErrorCode(-1);
		         _resp.setErrorMsg("code error");
		         logger.info("code error");
			}
        	
            _resp.setTokenResult(tokenResult);

        }catch (Exception e)
        {
            logger.error(e.getMessage());
            _resp.setErrorCode(-1);
            _resp.setErrorMsg("code error");
            logger.info("code error");
        }

        return _resp;
    }

	public IMiniService getMiniService() {
		return miniService;
	}

	public void setMiniService(IMiniService miniService) {
		this.miniService = miniService;
	}

	public IUserInfoService getUserInfoService() {
		return userInfoService;
	}

	public void setUserInfoService(IUserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}


}
